package com.marktotop.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 * MD5Util
 *
 * @author WXLD
 * @Data 2021/6/18
 * @Since 1.0
 */
@Component
public class MD5Util {

    /**
     * 自定义盐
     */
    private static final String SALT = "1a2b3c4d";

    public static String md5(String src){
        return DigestUtils.md5Hex(src);
    }

    /**
     * 模拟第一次加密
     *
     * @param inputPass
     * @return
     */
    private static String inputPassToFormPass(String inputPass){
        String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPass + SALT.charAt(5) + SALT.charAt(4);
        return md5(str);
    }

    /**
     * 模拟第二次加密
     *
     * @param formPass
     * @param salt
     * @return
     */
    public static String formPassToDBPass(String formPass, String salt){
        String str = "" + salt.charAt(0) + salt.charAt(2) + formPass + salt.charAt(5) + salt.charAt(4);
        return md5(str);
    }

    /**
     * 模拟直接两次加密过程
     *
     * @param inputPass
     * @param salt
     * @return
     */
    public static String inputPassToDBPass(String inputPass, String salt){
        String formPass = inputPassToFormPass(inputPass);
        String dbPass = formPassToDBPass(formPass, salt);
        return dbPass;
    }

    public static void main(String[] args) {
        String formPass = inputPassToFormPass("123456");
        System.out.println("第一次加密:"+formPass);
        System.out.println("第二次加密:"+formPassToDBPass(formPass,SALT));
        System.out.println("直接两次MD5加密:"+inputPassToDBPass("123456", SALT));
    }
}
